package uk.ac.cam.tcs40.sidnet.formal;

import uk.ac.cam.tcs40.sidnet.Hall;
import uk.ac.cam.tcs40.sidnet.R;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class AttendingFragment extends Fragment {

	private Formal m_Formal;

	private AttendeeAdapter m_Adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle extras = getActivity().getIntent().getExtras();

		String id;
		if (extras != null) {
			id = extras.getString("id");
			if (id != null) {
				m_Formal = Hall.getInstance().getFormal(id);
				new DownloadFormalAttendingTask().execute();
			}
		}
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		super.onCreateView(inflater, container, savedInstanceState);

		final View view = inflater.inflate(R.layout.activity_formal_attending, container, false);

		final ListView attending = (ListView) view.findViewById(R.id.attending);
		m_Adapter = new AttendeeAdapter(getActivity(), m_Formal.getAttendingList());
		attending.setAdapter(m_Adapter);

		return view;
	}

	private class DownloadFormalAttendingTask extends AsyncTask<Void, Void, Void> {
		protected void onPreExecute() {
		}

		protected Void doInBackground(Void... v) {
			Hall.getInstance().getFormalAttendingDetails(m_Formal);
			return null;
		}

		protected void onProgressUpdate(Void... progress) {
		}

		protected void onPostExecute(Void result) {
			m_Adapter.notifyDataSetChanged();
		}
	}
}
